package info.fqtech.interview.thread;

import java.util.concurrent.*;

/**
 * @author: jack
 * @date: 2018/12/18 17:5935
 * @version: 0.1
 * @description: 线程相关问题1
 * @modified: <修改说明>
 **/
public class ThreadQuestion1 {
    private static final int corePoolSize = 10;
    private static final int maximumPoolSize = corePoolSize;
    private static final long keepAliveTime = 10;
    private static final TimeUnit unit = TimeUnit.SECONDS;
    private static final int workQueueSize = 1;
    private static final BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(workQueueSize);
    private static final ThreadFactory threadFactory = Executors.defaultThreadFactory();
    // AbortPolicy如果堆积的工作数量超过了workQueue大小，就会抛异常
    // private static final RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();
    // 如果如果线程池不足，则调用主线程进行处理
    // private static final RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    // 如果没有多余的线程处理任务，则丢弃
    // private static final RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.DiscardOldestPolicy();
    // 丢弃
    private static final RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.DiscardPolicy();


    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, rejectedExecutionHandler);


    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            threadPoolExecutor.execute(new ThreadWorker1(5));
            int queueSize = threadPoolExecutor.getQueue().size();
            System.out.println("queueSize:" + queueSize);
        }
    }
}
